Skip to content

Give a button with no name a name of its own - #438

Merged
jakejackson1 merged 3 commits into
gravitypdffrom
fix/147-unnamed-button-fieldname
Sep 23, 2026
Merged

jakejackson1 merged 3 commits into
gravitypdffrom
fix/147-unnamed-button-fieldname

Conversation

@jakejackson1

Copy link
Copy Markdown
Member

Fixes #147

HTML does not require a button to have a name: a submit button without one triggers the form but is not sent with it. With useActiveForms on, mPDF warned on these buttons (and PHP 5–7 raised a notice), because the Form::print_ob_* methods read $objattr['fieldname'] without checking it was set. Tag\Input only sets that key when the element has a name.

What changed

The name read is guarded. The print_ob_* methods now read fieldname with the same isset() guard as the optional attributes beside it, falling back to ''. The hidden-input call in Tag\Input gets the same guard. A text, hidden, textarea, select, checkbox or radio field with no name is still refused, but now only by mPDF's own MpdfException ("must have a name attribute"), with no warning before it.

Unnamed buttons get distinct names. SetFormSubmit(), SetFormReset() and SetJSButton() now name an unnamed button Submit_<n>, Reset_<n> or Button_<n>, where <n> is the field's number. Image inputs go through SetJSButton(). A fixed name would merge buttons:

  • Before this change, an unnamed submit fell back to Submit and an unnamed reset to Reset, so two of either were written as two fields with the same /T. Viewers treat same-named fields as one field.
  • Inside mPDF, a button's JavaScript action (array_form_button_js) and image icon (form_button_icon) are looked up by name. A second button under the same name would replace the first button's action or icon.
  • An unnamed JS button or image input used to throw once the warning had fired, because a null name fails the name check. It now renders.

The number is formCount + 1, which is the n that SetFormButton() gives the field. formCount is part of Form's state snapshot, so a button measured for page-break-inside: avoid and then drawn again keeps the same name. I checked this by hand.

Named buttons are unchanged. The only visible difference is the /T of a button that had no name: Submit becomes Submit_1. Unnamed buttons are not submitted, so nothing depended on that value.

Snapshots

None changed. No snapshot has an unnamed button.

Verification

  • New tests/Mpdf/UnnamedButtonTest.php:
    • two unnamed submit, reset, JS and image buttons each become two fields with distinct names, with no warning;
    • two unnamed JS buttons each keep their own action;
    • each kind of unnamed non-button field (text, hidden, textarea, select, checkbox, radio) throws MpdfException and does not warn.
  • composer test: 3608 tests, 16039 assertions, OK (5 skipped, as on gravitypdf)
  • composer test -- --group=snapshot: 85 tests, OK
  • composer cs: clean
  • PHPStan 2.2: no errors
  • php -l under PHP 5.6 on every changed file

Upstream

The same code is in mpdf/mpdf, and the diff applies there as it stands.

🤖 Generated with Claude Code

jakejackson1 and others added 3 commits September 24, 2026 05:37
A submit, reset or JS button, or an image input, without a name read the missing fieldname
and warned. The print_ob_* methods now guard that read as they guard the attributes beside it,
and an unnamed button is named after its kind and field number (Submit_1, Reset_2, Button_3)
so two of them stay two fields with their own actions. Other unnamed fields are refused with
mPDF's own exception instead of a warning first.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…overlapping tests

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A submit, reset or JS button with no name warns: Form.php reads objattr["fieldname"] unguarded

1 participant